<?php

class PeopleController extends BackendController
{

    public $layout='/layouts/column2';
    public $city_en;
    public function actionIndex()
    {
        header("Content-type: text/html; charset=utf-8");
        $role = Yii::app()->request->getParam('role');
        $rid = Yii::app()->request->getParam('rid');
        if(Yii::app()->request->isPostRequest){

            $code_num = Yii::app()->request->getParam('code_num');
            $user_name = Yii::app()->request->getParam('user_name');
            $user_phone = Yii::app()->request->getParam('user_phone');
            $is_subject = Yii::app()->request->getParam('is_subject');
            $gid = Yii::app()->request->getParam('gid');
            $cid = $_POST['cid'];
            $model = new People();
            $criteria = new CDbCriteria;
            $criteria->addCondition("role=$role");
            if($code_num){
                $criteria->addSearchCondition('code_num', $code_num);
            }
            if($user_name){
                $criteria->addSearchCondition('user_name', $user_name);
            }
            if($user_phone){
                $criteria->addSearchCondition('user_phone', $user_phone);
            }
            if($is_subject){
                $criteria->addSearchCondition('is_subject', $is_subject);
            }
            if($gid){
                $criteria->addCondition("gid=$gid");
            }
            if($cid){
                $criteria->addCondition("cid=$cid");
            }
            $criteria->order = "id DESC";
            $dataProvider= new CActiveDataProvider($model, array(
                'criteria' => $criteria,
                'pagination'=>array(
                    'pageSize'=>20,
                ),
            ));
        }else{
            $cid = Yii::app()->request->getParam('cid');
            $model = new People();
            $criteria = new CDbCriteria;
            $criteria->addCondition("role=$role");
            if($rid){
                $criteria->addCondition("relation_id=$rid");
            }
            if($cid){
                $criteria->addCondition("cid=$cid");
            }
            $criteria->order = "id DESC";
            $dataProvider= new CActiveDataProvider($model, array(
                'criteria' => $criteria,
                'pagination'=>array(
                    'pageSize'=>20,
                ),
            ));
        }
        if($role==3){
            $this->render('index_3',array('dataProvider'=>$dataProvider));
        }elseif($role==2){
            $this->render('index_2',array('dataProvider'=>$dataProvider,'rid'=>$rid));
        }elseif($role==1){
            $grade = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
            $this->render('index_1',array('dataProvider'=>$dataProvider,'grade'=>$grade));
        }
    }
    public function actionDuduInfo(){
        header("Content-type: text/html; charset=utf-8");
        $pid = Yii::app()->request->getParam('pid');
        $user_info = People::model()->findByPk($pid);
        $model = new Quan();
        $criteria = new CDbCriteria;
        $criteria->addCondition("pid=$pid");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($model, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>50,
            ),
        ));
//        $jiaDb = Yii::app()->db->createCommand("SELECT SUM(point) as count FROM {{quan}} WHERE type=1 AND pid=$pid");
//        $jia = $jiaDb->queryRow();
//        $jianDb = Yii::app()->db->createCommand("SELECT SUM(point) as count FROM {{quan}} WHERE type=0 AND pid=$pid");
//        $jian = $jianDb->queryRow();
        $jiaDb = Yii::app()->db->createCommand("SELECT COUNT(*) as count FROM {{quan}} WHERE type=1 AND pid=$pid");
        $jia = $jiaDb->queryRow();
        $jianDb = Yii::app()->db->createCommand("SELECT COUNT(*) as count FROM {{quan}} WHERE type=0 AND pid=$pid");
        $jian = $jianDb->queryRow();
        $count = $jia['count'] - $jian['count'];
        $this->render('dudu_info',array('dataProvider'=>$dataProvider,'pid'=>$pid,'count'=>$count,'user_info'=>$user_info));
    }
    public function actionPointInfo(){
        header("Content-type: text/html; charset=utf-8");
        $pid = Yii::app()->request->getParam('pid');
        $user_info = People::model()->findByPk($pid);
        $model = new Point();
        $criteria = new CDbCriteria;
        $criteria->addCondition("pid=$pid");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($model, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>50,
            ),
        ));

        $jiaDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=1 AND pid=$pid");
        $jia = $jiaDb->queryRow();
        $jianDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=0 AND pid=$pid");
        $jian = $jianDb->queryRow();
        $count = $jia['count'] - $jian['count'];
        $this->render('point_info',array('dataProvider'=>$dataProvider,'pid'=>$pid,'count'=>$count,'user_info'=>$user_info));
    }
    public function isShow($data){
        $str = '';
        if($data->is_show==0){
            $str .= '<a onclick="setShow(\''.$data->id.'\')"  href="javascript:void(0);" style="color:red;" >设为显示</a>';
        }else{
            $str .= '<a style="color:green;"  onclick="setUnShow(\''.$data->id.'\')"  href="javascript:void(0);" >取消显示</a>';
        }
        return $str;

    }
    public function duduJJ($data){
        $str = '';
        if($data->type==0){
            $str .= '<span style="color:red;" >-</span>';
        }else{
            $str .= '<span style="color:green;" >+</span>';
        }
        return $str;

    }
    public function jJ($data){
        $str = '';
        if($data->type==0){
            $str .= '<span style="color:red;" >-</span>';
        }else{
            $str .= '<span style="color:green;" >+</span>';
        }
        return $str;

    }
    public function isPointTime($data){
        $str = date('Y-m-d',$data->create_time);
        return $str;
    }
    public function isSex($data){
        $str = '';
        if($data->sex==1){
            $str .= '男';
        }else{
            $str .= '女';
        }
        return $str;
    }
    public function isStartTime($data){
        $str = date('Y-m-d',$data->start_time);
        return $str;
    }
    public function isStudent($data){
        $str = '';
        $str .= '<a onclick="jifenInfo(\''.$data->id.'\')"  href="javascript:void(0);" style="color:red;" >积分信息</a><br/>';
        $str .= '<a onclick="jiazhangInfo(\''.$data->id.'\')"  href="javascript:void(0);" style="color:green;" >家长信息</a><br/>';
        $str .= '<a onclick="duduInfo(\''.$data->id.'\')"  href="javascript:void(0);" style="color:green;" >dudu券信息</a><br/>';
        return $str;
    }
    public function actionPointDelete(){
        $id = Yii::app()->request->getParam('id');
        $info = Point::model()->findByPk($id);
        if($info){
            $ok = $info->delete();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '删除数据成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '删除数据失败';
            }

        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }

    public function actionPointUpdate(){
        $id = Yii::app()->request->getParam('id');
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        //获取区域
        $info = Point::model()->findByPk($id);
        if(Yii::app()->request->isPostRequest){
            if($info){



                $info->setAttributes($_POST['Point']);
                if($info->update()){
                    //写入周边
                    $data['status'] = 1;
                    $data['message'] = '修改成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = $this->showError($info->getErrors());
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你无权作此操作';
            }
            echo json_encode($data);
            Yii::app()->end();
        }

        $this->layout = '/layouts/dialog';
        $this->render('point_update',array('info'=>$info));
    }





    public function actionDelete(){
        $id = Yii::app()->request->getParam('id');
        $info = People::model()->findByPk($id);
        if($info){
            $ok = $info->delete();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '删除数据成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '删除数据失败';
            }

        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }
    public function actionLoadClass(){
        $gid = Yii::app()->request->getParam('gid');
        $class = Classes::model()->findAll('gid=:gid and status=:status and is_show=:is_show',array(':gid'=>$gid,':status'=>1,':is_show'=>1));
        $str = '';
        if($class){
            foreach($class as $v){
                $str .= '<option value="'.$v->id.'">'.$v->title.'</option>';
            }
        }
        echo $str;
    }

    public function actionUpdate(){
        $id = Yii::app()->request->getParam('id');
        $role = Yii::app()->request->getParam('role');
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        //获取区域
        $info = People::model()->findByPk($id);
        if(Yii::app()->request->isPostRequest){
            if($info){
                if($_POST['passwd'] !=''){
                    $_POST['People']['passwd'] = sha1($_POST['passwd']);
                }
                $info->setAttributes($_POST['People']);
                if($info->update()){
                    //写入周边
                    $data['status'] = 1;
                    $data['message'] = '修改成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = $this->showError($info->getErrors());
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你无权作此操作';
            }
            echo json_encode($data);
            Yii::app()->end();
        }
        $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
        $this->layout = '/layouts/dialog';
        if($role==3){
            $this->render('update_3',array('info'=>$info,'grade'=>$grade_array));
        }elseif($role==2){
            $this->render('update_2',array('info'=>$info));
        }elseif($role==1){
            $this->render('update_1',array('info'=>$info,'grade'=>$grade_array));
        }
    }
    public function actionAdd(){
        if(Yii::app()->request->isPostRequest){
            $_POST['Classes']['create_time'] = time();
            $_POST['Classes']['is_show'] = 1;
            $model = new Classes();
            $model->setAttributes($_POST['Classes']);
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '发布班级成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            $grade_array = Grade::model()->findAll('is_show=:is_show and status=:status',array(':is_show'=>1,':status'=>1));
            if($grade_array){
                $this->layout = '/layouts/dialog';
                $this->render('add',array('grade'=>$grade_array));
            }else{
                exit('总后台添加年级数据');
            }
        }
    }
    public function actionAjaxReflash(){
        $pid = Yii::app()->request->getParam('pid');
        if(Yii::app()->request->isPostRequest){
            $info = People::model()->findByPk($pid);
            $jiaDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=1 AND pid=$pid");
            $jia = $jiaDb->queryRow();
            $jianDb = Yii::app()->db->createCommand("SELECT SUM(num) as count FROM {{point}} WHERE type=0 AND pid=$pid");
            $jian = $jianDb->queryRow();
            $count = $jia['count'] - $jian['count'];
            $info->point = $count;
            if($info->update()){
                $data['status'] = 1;
                $data['message'] = '刷新成功';
            }else{
                $data['status'] = 1;
                $data['message'] = '刷新失败';
            }
            echo json_encode($data);
            Yii::app()->end();
        }
    }
    public function actionPointAdd(){
        $pid = Yii::app()->request->getParam('pid');
        if(Yii::app()->request->isPostRequest){
            $_POST['Point']['create_time'] = time();
            $_POST['Point']['pid'] = intval($pid);
            $model = new Point();
            $model->setAttributes($_POST['Point']);
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '添加成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);


                $this->layout = '/layouts/dialog';
                $this->render('point_add',array('pid'=>$pid));

        }
    }
    public function actionDuduAdd(){
        $pid = Yii::app()->request->getParam('pid');
        if(Yii::app()->request->isPostRequest){
            $_POST['Quan']['create_time'] = time();
            $_POST['Quan']['pid'] = intval($pid);
            $model = new Quan();
            $model->setAttributes($_POST['Quan']);
            if($model->save()){
                $data['status'] = 1;
                $data['message'] = '添加成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);


            $this->layout = '/layouts/dialog';
            $this->render('quan_add',array('pid'=>$pid));

        }
    }
}